package org.aman.publish;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import org.aman.publish.util.CmdUtil;
import org.aman.publish.util.SshCmdUtil;
import org.aman.publish.util.SshFtpUtil;
import org.aman.publish.util.SshUtil;
import org.apache.commons.io.FileUtils;

import java.io.File;

/**
 * @author DaiJG
 * @title PublishMain功能描述
 * @Date created at 2019/8/29 16:36
 * @Description
 * @since 1.0.0
 */
public class PublishEbqDocMain {
    
    public static void pulishFront(Config config) throws Exception{
        File distPath = new File("E:/iwksp/amancode/zstart/zstart-blog/zstart-blog-front/dist");
        String targetPath = "/data/pro/zstart/zstart-blog-front";
        
        // 删除打包目录
        boolean delResult = CmdUtil.deleteDir(distPath);
        System.out.println("del:"+delResult);
        
        // 打包
        // cmd /c cd E:\iwksp\aman\zstart-front\zstart-blog-front && npm run build
        String cmd = "cmd /c cd " + distPath.getParentFile().getAbsolutePath() + " && npm run build && echo 'over'";
        CmdUtil.execute(cmd);

        // 删除服务器target目录
        Session session =  SshUtil.getSession(config.getIp(),22, config.getUser(), config.getPasswd());
        SshCmdUtil.execute(session,"rm -rf "+targetPath+"/*");
        SshUtil.close(null, session);
        System.out.println("delete finish");
        
        // 连接ftp
        SshFtpUtil ft = new SshFtpUtil();
        Session s = ft.getSession(config.getIp(), 22, config.getUser(), config.getPasswd());
        ChannelSftp sftp = ft.getSftpChannel(s);

        // 删除服务器target目录
//        使用远程命令删除
//        ft.deleteDir(sftp, targetPath);
//        System.out.println("delete finish");

        // 上传文件到服务器target目录
        ft.uploadDir(sftp, targetPath, distPath);
        System.out.println("upload Finish");
        ft.closeAll(sftp, s); //关闭连接
    }

    public static void pulishApi(Config config) throws Exception{

        String targetPath = "/data/pro/zstart/zstart-blog-api";
        String projectPath = "E:/iwksp/amancode/zstart/zstart-blog/zstart-blog-api";
        File distPath = new File(projectPath + "\\target\\zstart-blog-api.jar");

        // 打包
        boolean delResult = CmdUtil.deleteDir(new File(projectPath, "target"));
        System.out.println("del:"+delResult);
        String cmd = "cmd /c cd " + projectPath + " && mvn clean install";
        CmdUtil.execute(cmd);
        
        SshFtpUtil ft = new SshFtpUtil();
        Session s = ft.getSession(config.getIp(), 22, config.getUser(), config.getPasswd());
        ChannelSftp sftp = ft.getSftpChannel(s);

        // 删除target目录
        ft.delete(targetPath, distPath.getName(), sftp);
        System.out.println("delete finish");

        // 上传文件
        ft.uploadFile(sftp, targetPath, distPath);
        System.out.println("upload Finish");
        ft.closeAll(sftp, s); //关闭连接

        Session session =  SshUtil.getSession(config.getIp(),22, config.getUser(), config.getPasswd());
        SshCmdUtil.execute(session,"ps -ef|grep 'java -jar zstart-blog-api.jar'|grep -v grep|awk {'print $2'}|sed -e 's/^/kill -9 /g' | sh -");
        Thread.sleep(2000);
        SshCmdUtil.execute(session,"cd /data/pro/zstart/zstart-blog-api && ./start.sh");
        SshUtil.close(null, session);
        System.out.println("kill server finish");
    }
    
    public static void main(String[] args) throws Exception {
        pulishApi(Config.EbqDoc);
//        pulishFront(Config.EbqDoc);
    }


}
